package com.ruoyi.classai.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.classai.domain.AnalysisModeRel;
import com.ruoyi.classai.domain.EwcAnalysismodeRel;
import com.ruoyi.classai.service.AnalysisModeRelService;
import com.ruoyi.classai.service.EwcAnalysismodeRelService;
import com.ruoyi.classai.utils.group.OkHttpUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.classai.service.impl.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 用户登录接口
 * @author zl
 * @date 2025年02月27日 11:56
 */
@Slf4j
@Validated
@RestController
@Tag(name = "视频模型接口", description = "视频模型接口")
@RequestMapping("/api/file")
public class FileParse extends BaseController {
    @Autowired
    private AnalysisModeRelService analysisModeRelService;

    @Autowired
    private EwcAnalysismodeRelService ewcAnalysismodeRelService;


    @PostMapping("/parseFilePath")
    public AjaxResult parseFilePath( @Valid @RequestBody AnalysisModeRel analysisModeRel) throws IOException {

        String baseUrl = "http://1.116.37.233:9993/analyze_video";
        String pathParam = analysisModeRel.getFilePath();
        String typeParam = "hundle";
        String pathParamByDeal = pathParam;
        int index = pathParam.indexOf('/', pathParam.indexOf("://") + 3);
        if (index != -1) {
            // 截取斜杠之后的子字符串
            pathParamByDeal = pathParam.substring(index + 1);
            log.info(pathParamByDeal);
        }
        // 拼接 URL 参数
        String finalUrl = baseUrl + "?path=" + pathParamByDeal + "&type=" + typeParam;

        // 调用 OkHttpUtil 发送请求
        String responseResult = OkHttpUtil.sendPostRequest(finalUrl, "");

        ObjectMapper objectMapper = new ObjectMapper();
        JsonData jsonData = objectMapper.readValue(responseResult, JsonData.class);
        String taskId = jsonData.getTask_id();
        log.info("task_id 的值为: " + taskId);

        EwcAnalysismodeRel ewcAnalysismodeRel = new EwcAnalysismodeRel();
        ewcAnalysismodeRel.setOriginalFilepath(analysisModeRel.getFilePath());
        ewcAnalysismodeRel.setTaskId(taskId);
        ewcAnalysismodeRelService.saveIfNotExists(ewcAnalysismodeRel);

        return AjaxResult.success("查询成功", ewcAnalysismodeRel); // 将响应结果包含在返回中
    }


    static class JsonData {
        private String task_id;
        private String status_endpoint;

        public String getTask_id() {
            return task_id;
        }

        public void setTask_id(String task_id) {
            this.task_id = task_id;
        }

        public String getStatus_endpoint() {
            return status_endpoint;
        }

        public void setStatus_endpoint(String status_endpoint) {
            this.status_endpoint = status_endpoint;
        }
    }
}

















